package com.ztewatch.wearcontacts.presenter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog.Calls;

public class RejectedCallLogPresenter {
	private Activity mActivity;
	private List<CallDate> callDates;
	private CallDateListhener mCallDateListhener;

	public RejectedCallLogPresenter(Activity activity,
			CallDateListhener callDateListhener) {
		mActivity = activity;
		callDates = new ArrayList<RejectedCallLogPresenter.CallDate>();
		mCallDateListhener = callDateListhener;
	}

	private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

		@Override
		public void onLoaderReset(Loader<Cursor> arg0) {

		}

		@Override
		public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
			if (c != null) {
				while (c.moveToNext()) {
					CallDate callDate = new CallDate();
					int number_index = c.getColumnIndex(Calls.NUMBER);
					int date_index = c.getColumnIndex(Calls.DATE);
					if (number_index != -1) {
						String number = c.getString(number_index);
						callDate.setNumber(number);
					}
					if (date_index != -1) {
						long date = c.getLong(date_index);
						callDate.setDate(date);
					}
					if (callDates != null) {
						callDates.add(callDate);
					}
				}

				if (mCallDateListhener != null) {
					mCallDateListhener.requestCallDateList();
				}
			}
		}

		@Override
		public Loader<Cursor> onCreateLoader(int id, Bundle args) {
			CursorLoader cursorLoader = new CursorLoader(mActivity);
			configCursorLoader(cursorLoader, Calls.MISSED_TYPE, false, -1);
			return cursorLoader;
		}
	};

	private void configCursorLoader(CursorLoader cursorLoader, int callType,
			boolean newOnly, long newerThan) {
		Uri uri = Calls.CONTENT_URI
				.buildUpon()
				.appendQueryParameter(Calls.LIMIT_PARAM_KEY,
						Integer.toString(1000)).build();

		StringBuilder where = new StringBuilder();
		ArrayList<String> selectionArgs = new ArrayList<String>();

		if (newOnly) {
			where.append(Calls.NEW);
			where.append(" = 1");
		}

		if (callType > -1) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			// Add a clause to fetch only items of type voicemail.
			where.append(String.format("(%s = ?)", Calls.TYPE));
			// Add a clause to fetch only items newer than the requested date
			selectionArgs.add(Integer.toString(callType));
		}

		if (newerThan > 0) {
			if (where.length() > 0) {
				where.append(" AND ");
			}
			where.append(String.format("(%s > ?)", Calls.DATE));
			selectionArgs.add(Long.toString(newerThan));
		}
		if (where.length() > 0) {
			where.append(" AND ");
		}
		where.append(Calls.CACHED_NAME + " is null");

		final String selection = where.length() > 0 ? where.toString() : null;
		cursorLoader.setUri(uri);
		cursorLoader.setSelection(selection);
		cursorLoader.setSelectionArgs(selectionArgs.toArray(new String[0]));
		cursorLoader.setProjection(_PROJECTION);
		cursorLoader.setSortOrder(Calls.DEFAULT_SORT_ORDER);
	}

	public void onStart() {
		mActivity.getLoaderManager().initLoader(0, null, loaderCallbacks);
	}

	public void onDestroy() {
		mActivity.getLoaderManager().destroyLoader(0);
	}

	public static final String[] _PROJECTION = new String[] { Calls._ID, // 0
			Calls.NUMBER, // 1
			Calls.DATE, // 2
			Calls.DURATION, // 3
			Calls.TYPE, // 4
			Calls.CACHED_NAME, // 8
			Calls.CACHED_NUMBER_TYPE, // 9
			Calls.CACHED_NUMBER_LABEL // 10
	};

	public class CallDate {
		private String number;
		private long date;

		public String getNumber() {
			return number;
		}

		public void setNumber(String number) {
			this.number = number;
		}

		public long getDate() {
			return date;
		}

		public void setDate(long date) {
			this.date = date;
		}
	}

	public interface CallDateListhener {
		List<CallDate> requestCallDateList();
	}
}
